package helloWorld;

import helloWorld.infoRetriever.Album;
import helloWorld.infoRetriever.OnlineInfoRetriever;

import java.util.List;
import java.util.Vector;

import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.widgets.MTImage;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.tapProcessor.TapProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.zoomProcessor.ZoomProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.MTColor;
import org.mt4j.util.math.Vector3D;
import org.mt4j.util.math.Vertex;

import processing.core.PImage;



public class AlbumsScene extends AbstractScene{
	
	private List<SceneAlbum> albums;
	private AlbumGestureListener myGL;
	private Controller controller;
	private DBInterface dbInterface;
	private OnlineInfoRetriever oIR;
	private final static int IMAGE_WIDTH = 150;
	private final static int START_POS = 2;
	private int totalAlbums;
	private MTImage goBackButton;
	
	public AlbumsScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);
		controller = Controller.getInstance();
		dbInterface = DBInterface.getInstance();
		oIR = new OnlineInfoRetriever();
		
		albums = new Vector<SceneAlbum>();
		myGL = new AlbumGestureListener(mtApplication);
		this.getCanvas().setFrustumCulling(true);
		
		this.setClearColor(new MTColor(146, 150, 188, 255));
		this.getCanvas().setName("albumcanvas");
		
		String goBackUrl = "http://clipart.usscouts.org/library/BSA_Cub_Scouts/Cub_insignia/arrow_point_clipart_bw.gif";
		PImage pim = this.getMTApplication().loadImage(goBackUrl);
		goBackButton = new MTImage(pim, this.getMTApplication());
		goBackButton.setSizeXYGlobal(60, 60);
		goBackButton.setName("albumsceneBACK");
		this.getCanvas().addChild(goBackButton);
		goBackButton.setPositionGlobal(new Vector3D(getMTApplication().width/2, getMTApplication().height*9/10));
	}

	public void init() {
		List<String> albumNames = dbInterface.captureAlbumsFromArtist(controller.getCurrentArtist());
		totalAlbums = albumNames.size();
		oIR.retrieveArtist(controller.getCurrentArtist());
		for(int i = 0; i < albumNames.size(); ++i){
			Album al = oIR.addAlbum(albumNames.get(i));
			addAlbum(al.getTitle(), al.getImage());
		}
		
		String url = "http://www.student.kuleuven.be/~s0181392/mume/multitouch/alfbeeldingen/trash.jpg";
		MTImage garbage = new MTImage(getMTApplication().loadImage(url), getMTApplication());
		garbage.setName("album_garbage");
		int width = getMTApplication().width;
		int height = getMTApplication().height;
		garbage.setWidthXYGlobal((int)width/10f);
		garbage.setHeightXYGlobal((int)width/10f);
		garbage.unregisterAllInputProcessors();
		System.out.println(width);
		System.out.println(height);
		garbage.setPositionGlobal(new Vector3D((int)9*(width/10), (int)9*(height/10f)));
		this.getCanvas().addChild(garbage);
		
		getCanvas().registerInputProcessor(new ZoomProcessor(getMTApplication()));
		getCanvas().addGestureListener(ZoomProcessor.class, myGL);
		
		goBackButton.unregisterAllInputProcessors();
		goBackButton.registerInputProcessor(new TapProcessor(getMTApplication()));
		goBackButton.addGestureListener(TapProcessor.class, myGL);
	}
	
	public void addAlbum(String album, String image){
		SceneAlbum newAlbum = new SceneAlbum(getMTApplication().loadImage(image), getMTApplication(), album);
		newAlbum.setName("album_" + album);
		newAlbum.setWidthXYGlobal(IMAGE_WIDTH);
		newAlbum.setHeightXYGlobal(IMAGE_WIDTH);
		newAlbum.unregisterAllInputProcessors();
		newAlbum.registerInputProcessor(new TapProcessor(getMTApplication()));
		newAlbum.addGestureListener(TapProcessor.class, myGL);
		newAlbum.registerInputProcessor(new ZoomProcessor(getMTApplication()));
		newAlbum.addGestureListener(ZoomProcessor.class, myGL);
		newAlbum.registerInputProcessor(new DragProcessor(getMTApplication()));
		newAlbum.addGestureListener(DragProcessor.class, myGL);
		int hPos = (int) START_POS + (albums.size()-1);
		newAlbum.setPositionGlobal(new Vector3D(hPos*getMTApplication().width/(totalAlbums+1), getMTApplication().height/4f));	
		this.getCanvas().addChild(newAlbum);
		albums.add(newAlbum);
	}
	public void shutDown() {
		for(int i = 0; i < albums.size(); ++i)
		{
			albums.get(i).unregisterAllInputProcessors();
			albums.get(i).removeFromParent();
			albums.remove(i).destroy();
		}
		getCanvas().unregisterAllInputProcessors();
	}
	

}
